package com.pillar.handler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pillar.enty.Room;
import com.pillar.game.BizHandler;
import com.pillar.game.Cmd;
import com.pillar.request.BasePackage;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;

/**
 * @author pillar
 * @desc
 * @email: 347637454@qq.com
 * @createDate 2021-01-04 15:49
 */
@Slf4j
public class CreamRoomHandler extends AbstractCheckLoginHandler {

    @Override
    public boolean apply(BasePackage basePackage) {
        return Cmd.CREATE_ROOM.equals(basePackage.getCmd());
    }

    @Override
    public boolean consume(Channel channel, BasePackage basePackage) throws JsonProcessingException {
        super.consume(channel, basePackage);
        log.info("玩家创建房间={}",basePackage);
        Room room = null;
        if (null != (room = BizHandler.hall.findRoomByUuid(basePackage.getUuid()))){
            writeText(channel,"玩家已经存在房间中，房间ID="+room.getRoomId());
            return false;
        }
        //TODO 查询玩家游戏状态，如果在房间排队或者游戏中，直接返回游戏信息

        if (BizHandler.hall.fullRoom()){
            writeText(channel,"大厅房间已达上限");
            return false;
        }
        room = BizHandler.hall.createRoom(basePackage.getUuid());
        writeText(channel,"创建房间成功，房间ID="+room.getRoomId());
        return false;
    }
}
